<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<style type="text/css">
			div {
				width: 50px;
				height: 50px;
				border: 1px solid blue;
				float: left;
				margin-right: 10px;
			}
		</style>

		<script type="text/javascript">
			window.onload = function() {
				var my_div = document.getElementsByTagName("div");
				
				var i = 0;
				for( ;i <= my_div.length - 1; i++) {
					my_div[i].index=i;//固定写法,记住自己的位置
					
					my_div[i].onclick = function() {
						this.style.background = "pink";
						console.log("老板，我是老:" + this.index);
					}
				}
				console.log(i);    //循环执行完毕之后i的值已经是 6
			}
		</script>
	</head>

	<body>
		<div>1</div>
		<div>2</div>
		<div>3</div>
		<div>4</div>
		<div>5</div>
		<div>6</div>
	</body>

</html>